#include"single.h"

List *List::lt=NULL;

List::List():login_list() {}

List* List::get_list()
{
	if(lt==NULL)
		lt=new List();
	return lt;
}
void List::sd_meg(const string&str)
{
	login_list.sd_meg(str);
}

void List::list_insert(struct bufferevent*bfev,string&act,string&name)
{
	login_list.insert(bfev,act,name);
}
void List::list_erase(struct bufferevent* bfev)
{
	login_list.erase(bfev);
}

const string *List::get_act(const bufferevent*bfev)
{
	return login_list.get_act(bfev);
}

const string *List::get_name(const bufferevent*bfev)
{
	return login_list.get_name(bfev);
}

Recorder* Recorder::recorder=NULL;

Recorder* Recorder::get_recorder()
	{
		if(recorder==NULL)
			{
				recorder=new Recorder();
			}
		return recorder;
	}
	
Recorder::Recorder():err(),run(),dbg()
	{
		err.open("err.txt",ios::out | ios::app);
		run.open("run.txt",ios::out | ios::app);
		dbg.open("dbg.txt",ios::out | ios::app);
	}
	
void Recorder::err_rd(const char*str,int n)
	{
		err<<"The exit is beacuse : "<<str<<endl;
		exit(n);
	}
	
Recorder::~Recorder()
{
	if(recorder!=NULL)
		delete recorder;
	recorder=NULL;
	err.close();
	run.close();
	dbg.close();
}	

string Recorder::get_now_time()
{
	time_t t = time(0); 
    char tmp[64]; 
    strftime( tmp, sizeof(tmp), "%Y/%m/%d %X : ",localtime(&t) );
	return tmp;
}
void Recorder::run_rd(const char*str)
{
	string time=get_now_time();
	run<<time<<"   "<<str<<endl;
}

void Recorder::run_rd(const char*str,const bufferevent* bfev)
{
	const string *act=List::get_list()->get_act(bfev);
	if(act==NULL)
		run_rd(str);
	else
	{
		string time=get_now_time();
		run<<time<<"   "<<*act<<" : "<<str<<endl;
	}
		
}

void Recorder::dbg_rd(const char*str)
{
	string time=get_now_time();
	dbg<<time<<"   "<<str<<endl;
}

Mysql *Mysql::sql=NULL;

Mysql::Mysql():_mysql()
	{
		if(!connect())
			Recorder::get_recorder()->err_rd("Mysql connect failed!!!");
	}

bool Mysql::connect()
	{
		mysql_init(&_mysql);
		if(!mysql_real_connect(&_mysql,"119.29.178.70","root","186391","chat",0,NULL,0))
				return false;
		return true;
	}
		
Mysql::~Mysql()
	{
		mysql_close(&_mysql);
		if(sql!=NULL)
			delete sql;
		sql=NULL;
	}
		

Mysql *Mysql::get_Mysql()
	{
		if(sql==NULL)
			sql=new Mysql();
		return sql;
	}
	
MYSQL *Mysql::get_mysql()
	{
		return &_mysql;
	}